package com.campusify.service.impl;

import org.springframework.stereotype.Service;
import com.campusify.compents.PageResult;
import com.campusify.pojo.PrivatemessageExample;
import com.campusify.pojo.PrivatemessageExample.Criteria;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.campusify.mapper.PrivatemessageMapper;
import com.campusify.pojo.Privatemessage;
import com.campusify.service.PrivateMessageService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 服务实现层
 * @author Administrator
 *
 */
@Service
public class PrivateMessageServiceImpl implements PrivateMessageService {

	@Autowired
	private PrivatemessageMapper privateMessageMapper;
	
	/**
	 * 查询全部
	 */
	@Override
	public List<Privatemessage> findAll() {
		return privateMessageMapper.selectByExample(null);
	}

	/**
	 * 按分页查询
	 */
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);		
		Page<Privatemessage> page=   (Page<Privatemessage>) privateMessageMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 增加
	 */
	@Override
	public void add(Privatemessage privateMessage) {
		privateMessageMapper.insert(privateMessage);		
	}

	
	/**
	 * 修改
	 */
	@Override
	public void update(Privatemessage privateMessage){
		privateMessageMapper.updateByPrimaryKey(privateMessage);
	}	
	
	/**
	 * 根据ID获取实体
	 * @param id
	 * @return
	 */
	@Override
	public Privatemessage findOne(Integer messageID){
		return privateMessageMapper.selectByPrimaryKey(messageID);
	}

	/**
	 * 批量删除
	 */
	@Override
	public void delete(Integer[] messageIDs) {
		for(Integer messageID:messageIDs){
			privateMessageMapper.deleteByPrimaryKey(messageID);
		}		
	}
	
	
		@Override
	public PageResult findPage(Privatemessage privateMessage, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		
		PrivatemessageExample example=new PrivatemessageExample();
		Criteria criteria = example.createCriteria();
		
		if(privateMessage!=null){			
						if(privateMessage.getContent()!=null && privateMessage.getContent().length()>0){
				criteria.andContentLike("%"+privateMessage.getContent()+"%");
			}			if(privateMessage.getSendtime()!=null && privateMessage.getSendtime().length()>0){
				criteria.andSendtimeLike("%"+privateMessage.getSendtime()+"%");
			}	
		}
		
		Page<Privatemessage> page= (Page<Privatemessage>)privateMessageMapper.selectByExample(example);		
		return new PageResult(page.getTotal(), page.getResult());
	}
	
}
